package com.meihuak.cloud_ordering.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meihuak.cloud_ordering.bean.Business;
import com.meihuak.cloud_ordering.bean.Canteen;
import com.meihuak.cloud_ordering.bean.Dish;
import com.meihuak.cloud_ordering.bean.OrderTable;
import com.meihuak.cloud_ordering.mapper.BusinessMapper;
import com.meihuak.cloud_ordering.service.BusinessService;
import com.meihuak.cloud_ordering.service.CanteenService;
import com.meihuak.cloud_ordering.service.DishService;
import com.meihuak.cloud_ordering.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {
    @Autowired
    CanteenService canteenService;
    @Autowired BusinessService businessService;
    @Autowired
    DishService dishService;
    @Autowired
    OrderService orderService;



    /**
     * 通过id获取商家信息
     *
     * @param id
     * @return
     */
    @Override
    public Business getBusinessById(int id) {
        return this.getById(id);
    }

    /**
     * 通过餐厅id获取商家列表
     *
     * @param id 餐厅id
     * @return 商家列表
     */
    @Override
    public List<Business> listByCanteenId(int id) {
        QueryWrapper<Business> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("canteen_id",id)
                .orderByAsc("num");
        return this.list(queryWrapper);
    }

    /**
     * 通过星级排序
     *id
     * @param id 餐厅
     * @return 降序排列的商家列表
     */
    @Override
    public List<Business> listSort(int id,String name) {
        QueryWrapper<Business> queryWrapper = new QueryWrapper<>();
        if (name.equals("star_level")) {
            queryWrapper.eq("canteen_id", id)
                    .orderByDesc("star_level");
        }
        return this.list(queryWrapper);
    }

    /**
     * 获取当前商家的下单人数
     *
     * @param id 商家id
     * @return 人数
     */
    @Override
    public int getWaitStudentCountByBusinessId(int id) {
        QueryWrapper<OrderTable> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("business_id",id).eq("flag",0);
        return orderService.list(queryWrapper).size();
    }

    /**
     * 判断num是否已经使用
     *
     * @param num
     * @return
     */
    @Override
    public boolean numIsExist(int num) {
        QueryWrapper<Business> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("num",num);
        if (this.list(queryWrapper).size()!=0)
            return true;
        return false;
    }

    /**
     * 通过账号密码获取商家
     *
     * @param id
     * @param pwd
     * @return
     */
    @Override
    public boolean getBussinessByIdAndPwd(int id, String pwd) {
        QueryWrapper<Business>  queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("id",id)
                .eq("userkey",pwd)
                .last("limit 1");
        return this.list(queryWrapper).size()!=0;
    }


}
